﻿using LuaInterface;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Framework.UGUI
{
	public class UIClickListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
	{
		public static UIClickListener Get(GameObject go)
		{
			return go.OnceAddComponent<UIClickListener>();
		}

		public static UIClickListener GetWithPath(GameObject go, string path)
		{
			var targetTransform = go.transform.Find(path);
			if (targetTransform != null)
			{
				return Get(targetTransform.gameObject);
			}
			return null;
		}

		private GeneralCallBackWithParam _clickCallback;
		private GeneralCallBackWithParam _clickDownCallback;
		private GeneralCallBackWithParam _clickUpCallback;
		//必须同一只手指按下抬起，如果用两只手指完成这个操作，不会响应
		private bool _hasClickDown = false;

		public void AddClickListener(LuaFunction luaClick, LuaTable luaTarget, object param = null)
		{
			GeneralCallBackWithParam.GetOrReuse(ref _clickCallback, luaClick, luaTarget, param);
		}

		public void AddClickDownListener(LuaFunction luaClickDown, LuaTable luaTarget, object param = null)
		{
			GeneralCallBackWithParam.GetOrReuse(ref _clickDownCallback, luaClickDown, luaTarget, param);
		}

		public void AddClickUpListener(LuaFunction luaClickUp, LuaTable luaTarget, object param = null)
		{
			GeneralCallBackWithParam.GetOrReuse(ref _clickUpCallback, luaClickUp, luaTarget, param);
		}

		public void RemoveClickListener()
		{
			GeneralCallBackWithParam.RecycleInst(ref _clickCallback); 
		}

		public void RemoveClickDownListener()
		{
			GeneralCallBackWithParam.RecycleInst(ref _clickDownCallback);
		}
		public void RemoveClickUpListener()
		{
			GeneralCallBackWithParam.RecycleInst(ref _clickUpCallback);
		}

		public void OnPointerClick(PointerEventData eventData)
		{
			if (!Application.isMobilePlatform || Input.touchCount <= 1)
			{
				if (_clickCallback != null)
				{
					GeneralCallBackWithParam.Invoke(ref _clickCallback, eventData.position, eventData.delta);
				}
			}
		}

		public void OnPointerDown(PointerEventData eventData)
		{
			if (!Application.isMobilePlatform || Input.touchCount <= 1)
			{
				_hasClickDown = true;
				if (_clickDownCallback != null)
				{
					GeneralCallBackWithParam.Invoke(ref _clickDownCallback, eventData.position, eventData.delta);
				}
			}
		}

		public void OnPointerUp(PointerEventData eventData)
		{
			if (!Application.isMobilePlatform || Input.touchCount <= 1 || _hasClickDown)
			{
				_hasClickDown = false;
				if (_clickUpCallback != null)
				{
					GeneralCallBackWithParam.Invoke(ref _clickUpCallback, eventData.position, eventData.delta);
				}
			}
		}

		void OnDestroy()
		{
			if (_clickCallback != null)
			{
				GeneralCallBackWithParam.RecycleInst(ref _clickCallback);
				UIEventDebugger.LogUIEventError("UIClickListener.click", gameObject);
			}
			if (_clickDownCallback != null)
			{
				GeneralCallBackWithParam.RecycleInst(ref _clickDownCallback);
				UIEventDebugger.LogUIEventError("UIClickListener.clickDown", gameObject);
			}
			if (_clickUpCallback != null)
			{
				GeneralCallBackWithParam.RecycleInst(ref _clickUpCallback);
				UIEventDebugger.LogUIEventError("UIClickListener.clickUp", gameObject);
			}
		}
	}
}